import numpy as np
import os

def read_data(data_dir, angle_file, signal_file):
  """
  读取指定角度下的数据，并进行整合

  Args:
    data_dir: 数据文件夹路径
    angle_file: 角度数据文件名
    signal_file: 信号数据文件名

  Returns:
    array_signal: 整合后的信号数据，形状为(num_sensors, num_samples)
    angle: 对应角度值 (取平均值)
  """
  angle_data = np.loadtxt(os.path.join(data_dir, "阵列罗盘航向姿态角数据", angle_file))
  signal_data = np.loadtxt(os.path.join(data_dir, "换能器信号数据", signal_file))

  # 计算角度平均值
  angle = np.mean(angle_data)

  # 整合信号数据
  num_sensors = 3  # 三元矢量水听器
  num_samples = signal_data.shape[0]
  array_signal = signal_data.reshape(num_samples, num_sensors, 3)  # (num_samples, num_sensors, 3)

  # 按照每个水听器构建复数信号
  complex_signals = []
  for i in range(num_sensors):
      complex_signal = array_signal[:, i, 0] + 1j * array_signal[:, i, 1]  # x + 1j * y
      complex_signals.append(complex_signal)

  array_signal = np.array(complex_signals)  # (num_sensors, num_samples)

  return array_signal, angle